const async = require('async');
const qxData = require('./qx_data');
const qxMysql = require('./qx_mysql');

const get_data = function (host, user, password, database, startDate) {
  let loop = 0;
  let date_list = qxData.getDays(new Date(startDate));
  let qxDao = new qxMysql.MySQLDb(host, user, password, database);

  async.eachSeries(qxData.areaList, function (area, callback) {
    console.log(area.index, '==========', area.dt, area.name, area.id, '==========', new Date().toLocaleString());

    let history_list = [];
    async.eachSeries(qxData.move_type, function (m, m_callback) {
      console.log(' >', m, '----------', new Date().toLocaleString());

      let endDate = qxData.getYestoday();
      let historycurve_list = qxData.getHistorycurve(area.dt, area.id, m, '20190112', endDate);
      let history_content = JSON.stringify(historycurve_list);

      qxDao.insertHistories([
        [area.id, area.dt, area.name, m, '20190112', endDate, history_content]
      ], function () {
        console.log('  -', 'historyCurve, OK~', new Date().toLocaleString());
        console.log('  -', 'rank...', new Date().toLocaleString());

        let city_ranks = [];
        let province_ranks = [];
        async.map(date_list, function (d, d_callback) {

          async.parallel({
            getCityrank: function (g_callback) {
              qxData.getCityrank_async(area.dt, area.id, m, d, function (data) {
                let rank_content = JSON.stringify(data);
                city_ranks.push([area.id, area.dt, area.name, m, d, rank_content]);
                g_callback(null, 1);
              });
            },
            getProvincerank: function (g_callback) {
              qxData.getProvincerank_async(area.dt, area.id, m, d, function (data) {
                let rank_content = JSON.stringify(data);
                province_ranks.push([area.id, area.dt, area.name, m, d, rank_content]);
                g_callback(null, 2);
              });
            }
          }, function (err, results) {
            d_callback();
          });

        }, function (err, results) {
          async.parallel({
            saveCityrank: function (s_callback) {
              qxDao.insertCityranks(city_ranks, function () {
                s_callback(null, 1);
              });
            },
            saveProvincerank: function (s_callback) {
              qxDao.insertProvinceranks(province_ranks, function () {
                s_callback(null, 2);
              });
            }
          }, function (err, results) {
            console.log('  -', 'rank, OK~', new Date().toLocaleString());
            m_callback();
          });
        });
      });
    }, function (err) {
      if (err) {
        console.log(' >', area.dt, area.name, 'ERROR:', err);
      } else {
        console.log(' >', area.dt, area.name, 'OK~');
        callback();
      }
    });
  }, function (err) {
    if (err) {
      console.log('处理出错');
    } else {
      console.log('处理完成');
    }
  });
};

get_data('localhost', 'root', 'root', 'qx', '2020-02-16');